<template>
  <div>
    <div class="box-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>电子处方</el-breadcrumb-item>
        <el-breadcrumb-item>药品资料</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-autocomplete
            v-model="formInline"
            :fetch-suggestions="querySearchAsync"
            placeholder="药品名称"
            :trigger-on-focus="false"
            @select="handleSelect">
          </el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="inquire(formInline)" icon="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div><!-- 药品信息表 -->
      <el-table :data="tableData" border stripe>
        <el-table-column property="name" label="药品名称"></el-table-column>
        <el-table-column inline-template label="药品分类">
          <div>{{ row.drug_classification[1] }}</div>
        </el-table-column>
        <el-table-column inline-template label="大单位">
          <div>{{ row.big_unit[1] }}{{ row.big_unit[0] }}</div>
        </el-table-column>
        <el-table-column property="ratio" label="换算量"></el-table-column>
        <el-table-column inline-template label="小单位">
          <div>{{ row.small_unit[1] }}{{ row.small_unit[0] }}</div>
        </el-table-column>
        <el-table-column inline-template label="剂量单位">
          <div>{{ row.drug_type[1] }}</div>
        </el-table-column>
        <el-table-column property="drug_spec" label="药品规格"></el-table-column>
        <el-table-column property="drug_content" label="药品含量"></el-table-column>
        <el-table-column inline-template label="来源">
          <div>
            <div v-if="row.source == 0">国产</div>
            <div v-if="row.source == 1">进口</div>
            <div v-if="row.source == 2">合资</div>
            <div v-if="row.source == 3">本院</div>
          </div>
        </el-table-column>
        <el-table-column property="qty_available" label="库存量"></el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: '',
      total: 1,
      tableData: [],
      CurrPage: '',
      queryStatus: ''
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.CurrPage = val
      if (this.queryStatus == '') {
        this.$http.get('/api/drugs/page/'+val).then(function (response) {
          this.total = response.data.total
          this.tableData = response.data.data
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      } else {
        let parameters = this.queryStatus+'/'+val
        this.$http.get('/api/drugs/'+parameters).then(function (response) {
          this.total = response.data.total
          this.tableData = response.data.data
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      }
    },
    inquire(formInline) {
      if (formInline == '') {
        this.$message({
          showClose: true,
          message: '请输入查询条件',
          type: 'warning'
        })
      }
      else {
        this.$http.get('/api/drugs/'+formInline+'/1').then(function (response) {
          this.total = response.data.total
          this.tableData = response.data.data
          this.queryStatus = formInline
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      }
    },
    querySearchAsync(queryString, funcd) {
      if (queryString == '') {
        funcd()
        this.queryStatus = ''
        this.handleCurrentChange(1)
      }
      else {
        this.$http.get('/api/drugs/'+queryString+'/1').then(function (response) {
          let data = []
          let datas = response.data.data
          for (let i = 0; i < datas.length; i++) {
            data[i] = { 'value': datas[i].name }
          }
          if (data.length > 0) {
            console.log(data)
            funcd(data)  
          } else {
            data[0] = { 'value': '' }
            funcd(data)
          }         
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
          funcd()
        })       
      }
    },
    handleSelect(item) {
      this.inquire(item.value)
    }
  },  
  mounted() {
    this.handleCurrentChange(1)
  }
}
</script>